# -*- coding: utf-8 -*-
"""
Created on Thu Dec 12 16:05:57 2013

@author: Liang Zhang
"""

from scipy import linalg
import numpy as np
           
def __WHT(data):
    '''
    Walsh hadamard transiton
    data^hadamard=>remain
    '''
    _data=np.array(data,dtype=np.int,copy=True)
    _data[_data==2]=-1
    h=linalg.hadamard(_data.shape[0],dtype=np.int)    
    f=h.dot(_data)
    #generate select code
    _f=np.abs(f)
    scode=np.argmax(_f,axis=0)
    flip=f[scode,np.arange(scode.size)]<0
    '''return the remain matrix for coding'''
    #this generate from hadware
    _h=h[:,scode]
    _h[_h==-1]=0#-1->0
    _h^=flip
    _h[_h==1]=3#1->C
    #get remain matrix
    remain=data^_h
    remain[remain==3]=0#C->X
    return remain

def WHT(data):
    '''
    align data
    '''
    align_size=1<<(data.shape[0]-1).bit_length()
    _data=data.copy()
    _data.resize((align_size,data.shape[1]))
    return __WHT(_data)[:data.shape[0],:]
    
if __name__=='__main__':
    from time import clock
    from testset import testset
    t0=clock()    
    ts=testset('F:/work/vlsi-test/TestDataCompression/circutes/s5378')
